package com.rocky.project.action.model;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.opensymphony.webwork.ServletActionContext;
import com.rocky.project.action.base.BaseAction;
import com.rocky.project.model.family.Archive;
import com.rocky.project.util.WebUtil;

public class JasperAction extends BaseAction {

	private static final long serialVersionUID = -8594184834633165967L;

	private HSSFWorkbook workbook;

	private Archive archive;
	private JRDataSource  attDS;
	
	public JRDataSource getAttDS() {
		return attDS;
	}

	public void setAttDS(JRDataSource attDS) {
		this.attDS = attDS;
	}

	public Archive getArchive() {
		return archive;
	}

	public void setArchive(Archive archive) {
		this.archive = archive;
	}

	public HSSFWorkbook getWorkbook() {
		return workbook;
	}

	public void setWorkbook(HSSFWorkbook workbook) {
		this.workbook = workbook;
	}

	@SuppressWarnings("unchecked")
	public String jasper() throws Exception {
		
		String id = WebUtil.getParaValue("id");

		this.archive = (Archive) baseService.get(Archive.class, Long.valueOf(id));
		log.debug("获取archive对象成功，准备初始化数据给jasperreport！");
		
//		String json = JSONUtil.bean2Json(this.archive , JSONUtil.DEFAULT_EXCLUDES, JSONUtil.DEFAULT_DATEPATTERN).toString();
//		log.info(json);
//		String json = FreemarkerUtil.getInstance().doExecute("/template/archive_report.ftl", ServletActionContext.getContext().getActionInvocation());		
//		this.archive = (Archive) JSONObject.toBean(JSONObject.fromObject(json), Archive.class);
		this.archive.setDescription(ServletActionContext.getServletContext().getRealPath("/"));

		this.attDS=new JRBeanCollectionDataSource(this.archive.getPics());

//	    try {
//			JasperCompileManager.compileReportToFile(
//					"src/main/webapp/our_jasper_template.jrxml",
//					"src/main/webapp//our_compiled_template.jasper");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return ERROR;
//		}
	    
		if(StringUtils.isNotBlank(this.suc)){
			return this.suc;
		}
		return SUCCESS;	
	}

	public String excel() throws Exception {
		try {
			workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet();

			workbook.setSheetName(0, "厂商产品", (short) 1);
			HSSFRow row = sheet.createRow((short) 0);

			HSSFCell cell0 = row.createCell((short) 0);
			HSSFCell cell1 = row.createCell((short) 1);
			HSSFCell cell2 = row.createCell((short) 2);
			HSSFCell cell3 = row.createCell((short) 3);
			HSSFCell cell4 = row.createCell((short) 4);
			HSSFCell cell5 = row.createCell((short) 5);
			HSSFCell cell6 = row.createCell((short) 6);
			HSSFCell cell7 = row.createCell((short) 7);
			HSSFCell cell8 = row.createCell((short) 8);
			HSSFCell cell9 = row.createCell((short) 9);

			cell0.setEncoding(HSSFCell.ENCODING_UTF_16);//这里是设置编码保证中文正常显示
			cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell9.setEncoding(HSSFCell.ENCODING_UTF_16);

			cell0.setCellValue("厂商名");
			cell1.setCellValue("产品名");
			cell2.setCellValue("重量");
			cell3.setCellValue("星级");
			cell4.setCellValue("parama");
			cell5.setCellValue("paramb");
			cell6.setCellValue("paramc");
			cell7.setCellValue("paramd");
			cell8.setCellValue("状态");
			cell9.setCellValue("备注");

		} catch (Exception e) {
		}
		return SUCCESS;
	}
}
